﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace ISUM_V2
{
	/// <summary>
	/// Interaction logic for UCOption.xaml
	/// </summary>
	public partial class UCOption : UserControl
	{
        private MorseCodeFileModule _Fsm;
        private SrzKeyboardOption _KeyboardOption;
        private SrzMouseOption _MouseOption;
        private SrzDefaultOption _DefaultOption;
        private ScreenKeyboard _Screenkeyboard;
        private ScreenMouse _Screenmouse;

        private int _SelectedSize = 0;
        private int _SelectedSizeMouse = 0;

		public UCOption(ScreenKeyboard screenkeyboard, ScreenMouse screenmouse)
		{
			this.InitializeComponent();
            this._Screenkeyboard = screenkeyboard;
            this._Screenmouse = screenmouse;

            Init();
            
		}

        private void Init()
        {
            Btn_init.Click += new RoutedEventHandler(Btn_init_Click);
            Btn_Apply.Click += new RoutedEventHandler(Btn_Apply_Click);
            //Btn_Save.Click += new RoutedEventHandler(Btn_Save_Click);
            //Btn_Exit.Click += new RoutedEventHandler(Btn_Exit_Click);

            Check_AutoComplete.Click += new RoutedEventHandler(Check_AutoComplete_Click);
            Check_Language.Click += new RoutedEventHandler(Check_Language_Click);
			
			Radio_Large.Click +=new System.Windows.RoutedEventHandler(Radio_Large_Click);
            Radio_Normal.Click += new RoutedEventHandler(Radio_Normal_Click);
            Radio_Small.Click += new RoutedEventHandler(Radio_Small_Click);

            Radio_Large_Mouse.Click += new RoutedEventHandler(Radio_Large_Mouse_Click);
            Radio_Normal_Mouse.Click += new RoutedEventHandler(Radio_Normal_Mouse_Click);
            Radio_Small_Mouse.Click += new RoutedEventHandler(Radio_Small_Mouse_Click);

            Btn_MouseRangeDown.Click += new RoutedEventHandler(Btn_MouseRangeDown_Click);
            Btn_MouseRangeUp.Click += new RoutedEventHandler(Btn_MouseRangeUp_Click);
            Btn_WheelRangeDown.Click += new RoutedEventHandler(Btn_WheelRangeDown_Click);
            Btn_WheelRangeUp.Click += new RoutedEventHandler(Btn_WheelRangeUp_Click);

            Check_AutoHidden.Click += new RoutedEventHandler(Check_AutoHidden_Click);
            Check_AutoStart.Click += new RoutedEventHandler(Check_AutoStart_Click);
            Check_SignalCheck.Click += new RoutedEventHandler(Check_SignalCheck_Click);

            Btn_DeleteCon.Click += new RoutedEventHandler(Btn_DeleteCon_Click);

            _Fsm = MorseCodeFileModule.getInstance();
            _KeyboardOption = (SrzKeyboardOption)_Fsm.readFile(MainWindow.GetDoubleParent() + "/SrzMoreseCode/KeyboardOption.data");
            _MouseOption = (SrzMouseOption)_Fsm.readFile(MainWindow.GetDoubleParent() + "/SrzMoreseCode/MouseOption.data");
            _DefaultOption = (SrzDefaultOption)_Fsm.readFile(MainWindow.GetDoubleParent() + "/SrzMoreseCode/DefaultOption.data");

            Check_AutoComplete.IsChecked = _KeyboardOption.IsCheckAutoComplete;
            Check_Language.IsChecked = _KeyboardOption.IsCheckKorean;

            Check_AutoHidden.IsChecked = _DefaultOption.IsAutoHidden;
            Check_AutoStart.IsChecked = _DefaultOption.IsAutoStart;
            Check_SignalCheck.IsChecked = _DefaultOption.IsSignalCheck;

            tbMouseMoveValue.Text = _MouseOption.CurrentMoveRage.ToString();
            tbMouseWheelValue.Text = _MouseOption.CurrentWheelRage.ToString();

            textMouseRange.Text = _MouseOption.CurrentMoveRage.ToString();
            textWheelRange.Text = _MouseOption.CurrentWheelRage.ToString();

            SelectRadio(_KeyboardOption.IsCheckMaxSize);
            SelectRadioMouse(_MouseOption.IsCheckMaxSize);
            Check_AutoComplete_Click(null, null);
            Check_Language_Click(null, null);
        }

      

        void Btn_DeleteCon_Click(object sender, RoutedEventArgs e)
        {
            UC_MessageBox.Visibility = Visibility.Visible;
            UC_MessageBox.Tb_Message.Text = "연결 디바이스를 다시" + "\r\n선택하시겠습니까?";
            UC_MessageBox.Btn_Cancel.Click +=new RoutedEventHandler(Btn_Cancel_Click);    
            UC_MessageBox.Btn_OK.Click += new RoutedEventHandler(Btn_OK_Click);
        }

        void Btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            UC_MessageBox.Visibility = Visibility.Hidden;
        }

        void Btn_OK_Click(object sender, RoutedEventArgs e)
        {
            System.IO.File.Delete(MainWindow.GetDoubleParent() + "/Connect.data");
            UC_MessageBox.Visibility = Visibility.Hidden;

            string processName = "ISUM_V2"; // .exe 는 빼셔야 되여~

            Process[] processes = Process.GetProcessesByName(processName);

            foreach (Process process in processes)
            {

                process.Kill();

            }
        }

        void Check_SignalCheck_Click(object sender, RoutedEventArgs e)
        {
            
        }

        void Check_AutoStart_Click(object sender, RoutedEventArgs e)
        {
            
        }

        void Check_AutoHidden_Click(object sender, RoutedEventArgs e)
        {
            
        }

        void Btn_WheelRangeUp_Click(object sender, RoutedEventArgs e)
        {
            int temp = int.Parse(textWheelRange.Text);
            if (temp < 5)
            {
                temp++;
                textWheelRange.Text = temp.ToString();
                tbMouseWheelValue.Text = temp.ToString();
            }
        }

        void Btn_WheelRangeDown_Click(object sender, RoutedEventArgs e)
        {
            int temp = int.Parse(textWheelRange.Text);
            if (temp != 1)
            {
                temp--;
                textWheelRange.Text = temp.ToString();
                tbMouseWheelValue.Text = temp.ToString();
            }
        }

        void Btn_MouseRangeUp_Click(object sender, RoutedEventArgs e)
        {
            int temp = int.Parse(textMouseRange.Text);
            if (temp < 20)
            {
                temp++;
                textMouseRange.Text = temp.ToString();
                tbMouseMoveValue.Text = temp.ToString(); 
            }
        }

        void Btn_MouseRangeDown_Click(object sender, RoutedEventArgs e)
        {
            int temp = int.Parse(textMouseRange.Text);
            if (temp != 1)
            {
                temp--;
                textMouseRange.Text = temp.ToString();
                tbMouseMoveValue.Text = temp.ToString();
            }
        }

        private void SelectRadio(int selectedNumber)
        {
            switch (selectedNumber)
            {
                case 0: Radio_Small_Click(null, null); break;
                case 1: Radio_Normal_Click(null, null); break;
                case 2: Radio_Large_Click(null, null); break;
            }
        }

        private void SelectRadioMouse(int selectedNumber)
        {
            switch (selectedNumber)
            {
                case 0: Radio_Small_Mouse_Click(null, null); break;
                case 1: Radio_Normal_Mouse_Click(null, null); break;
                case 2: Radio_Large_Mouse_Click(null, null); break;
            }
        }

        #region Radio Event
        void Radio_Small_Click(object sender, RoutedEventArgs e)
        {
            ShowWindowSizeText(0);
        }

        void Radio_Normal_Click(object sender, RoutedEventArgs e)
        {
            ShowWindowSizeText(1);
        }

        void Radio_Large_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ShowWindowSizeText(2);
        }

        private void ShowWindowSizeText(int selectNumber)
        {
            Btn_WindowSize.TabIndex = 100;
            Btn_WindowSize.TabIndex = 50;
            switch (selectNumber)
            {
                case 0 :
                    _SelectedSize = 0;
                    Radio_Small.IsChecked = true;
                    tbWindowSizeSmall.Visibility = Visibility.Visible;
                    tbWindowSizeNormal.Visibility = Visibility.Hidden;
                    tbWindowSizeLarge.Visibility = Visibility.Hidden;
                    break;
                case 1 :
                    _SelectedSize = 1;
                    Radio_Normal.IsChecked = true;
                    tbWindowSizeSmall.Visibility = Visibility.Hidden;
                    tbWindowSizeNormal.Visibility = Visibility.Visible;
                    tbWindowSizeLarge.Visibility = Visibility.Hidden;
                    break;
                case 2 :
                    _SelectedSize = 2;
                    Radio_Large.IsChecked = true;
                    tbWindowSizeSmall.Visibility = Visibility.Hidden;
                    tbWindowSizeNormal.Visibility = Visibility.Hidden;
                    tbWindowSizeLarge.Visibility = Visibility.Visible;
                    break;
            }
        }

        void Radio_Small_Mouse_Click(object sender, RoutedEventArgs e)
        {
            ShowWindowSizeTextMouse(0);
        }

        void Radio_Normal_Mouse_Click(object sender, RoutedEventArgs e)
        {
            ShowWindowSizeTextMouse(1);
        }

        void Radio_Large_Mouse_Click(object sender, RoutedEventArgs e)
        {
            ShowWindowSizeTextMouse(2);
        }

        private void ShowWindowSizeTextMouse(int selectNumber)
        {
            Btn_WindowSize1.TabIndex = 100;
            Btn_WindowSize1.TabIndex = 50;
            switch (selectNumber)
            {
                case 0:
                    _SelectedSizeMouse = 0;
                    Radio_Small_Mouse.IsChecked = true;
                    tbWindowSizeSmall1.Visibility = Visibility.Visible;
                    tbWindowSizeNormal1.Visibility = Visibility.Hidden;
                    tbWindowSizeLarge1.Visibility = Visibility.Hidden;
                    break;
                case 1:
                    _SelectedSizeMouse = 1;
                    Radio_Normal_Mouse.IsChecked = true;
                    tbWindowSizeSmall1.Visibility = Visibility.Hidden;
                    tbWindowSizeNormal1.Visibility = Visibility.Visible;
                    tbWindowSizeLarge1.Visibility = Visibility.Hidden;
                    break;
                case 2:
                    _SelectedSizeMouse = 2;
                    Radio_Large_Mouse.IsChecked = true;
                    tbWindowSizeSmall1.Visibility = Visibility.Hidden;
                    tbWindowSizeNormal1.Visibility = Visibility.Hidden;
                    tbWindowSizeLarge1.Visibility = Visibility.Visible;
                    break;
            }
        }

        #endregion

        void Check_Language_Click(object sender, RoutedEventArgs e)
        {
            ShowCanvasKoreanDefault();
        }

        

        void Check_AutoComplete_Click(object sender, RoutedEventArgs e)
        {
            ChangeKeyboardBackground(Btn_AutoCompleteOnOff);
        }

        void Btn_Exit_Click(object sender, RoutedEventArgs e)
        {
            
        }

        //void Btn_Save_Click(object sender, RoutedEventArgs e)
        //{
        //    SaveKeyboardOption();
        //    SaveMouseOption();
        //    SaveDefaultOption();
        //}

        public void SaveDefaultOption()
        {
            //SrzDefaultOption _DefaultOption = new SrzDefaultOption();
            //_DefaultOption.AutoHidden = false;
            //_DefaultOption.AutoStart = true;
            //_DefaultOption.SignalCheck = true;

            _DefaultOption.IsAutoHidden = (bool)Check_AutoHidden.IsChecked;
            _DefaultOption.IsAutoStart = (bool)Check_AutoStart.IsChecked;
            _DefaultOption.IsSignalCheck = (bool)Check_SignalCheck.IsChecked;


            _Fsm.writeFile(MainWindow.GetDoubleParent() + "/SrzMoreseCode/DefaultOption.data", _DefaultOption);
        }

        public void SaveMouseOption()
        {
            //SrzMouseOption _MouseOption = new SrzMouseOption();
            //_MouseOption.MoveRage = int.Parse(textMouseRange.Text);
            //_MouseOption.WheelRage = int.Parse(textWheelRange.Text);
            //_MouseOption.IsCheckMaxSize = _SelectedSizeMouse;

            _MouseOption.CurrentMoveRage = int.Parse(textMouseRange.Text);
            _MouseOption.CurrentWheelRage = int.Parse(textWheelRange.Text);
            _MouseOption.IsCheckMaxSize = _SelectedSizeMouse;

            _Fsm.writeFile(MainWindow.GetDoubleParent() + "/SrzMoreseCode/MouseOption.data", _MouseOption);
        }

        public void SaveKeyboardOption()
        {
            //SrzKeyboardOption _KeyboardOption = new SrzKeyboardOption();
            //_KeyboardOption.AutoComplete = (bool)Check_AutoComplete.IsChecked;
            //_KeyboardOption.Korean = (bool)Check_Language.IsChecked;
            //_KeyboardOption.MaxSize = _SelectedSize;

            _KeyboardOption.IsCheckAutoComplete = (bool)Check_AutoComplete.IsChecked;
            _KeyboardOption.IsCheckKorean = (bool)Check_Language.IsChecked;
            _KeyboardOption.IsCheckMaxSize = _SelectedSize;
        
            _Fsm.writeFile(MainWindow.GetDoubleParent() + "/SrzMoreseCode/KeyboardOption.data", _KeyboardOption);
        }
        private delegate void NoParamDelegate();
        void Btn_Apply_Click(object sender, RoutedEventArgs e)
        {
            SaveKeyboardOption();
            SaveMouseOption();
            SaveDefaultOption();

            _Screenkeyboard.LoadKeyboardOption();
            _Screenkeyboard.UpdateAutoCompleteText();
            _Screenmouse.SetMouseOption();
            _Screenmouse.LoadDefaultOption();
            
          ////  this.Dispatcher.BeginInvoke(new NoParamDelegate(_Screenkeyboard.UpdateAutoCompleteText));
          //  _Screenkeyboard.LoadDefaultOption();
          //  _Screenmouse.LoadDefaultOption();
            
        }

        void Btn_init_Click(object sender, RoutedEventArgs e)
        {
            // 키보드 부분
            Check_AutoComplete.IsChecked = _KeyboardOption.AutoComplete;
            Check_Language.IsChecked = _KeyboardOption.Korean;
            SelectRadio(_KeyboardOption.MaxSize);

            // 마우스 부분
            tbMouseMoveValue.Text = _MouseOption.CurrentMoveRage.ToString();
            tbMouseWheelValue.Text = _MouseOption.CurrentWheelRage.ToString();

            textMouseRange.Text = _MouseOption.MoveRage.ToString();
            textWheelRange.Text = _MouseOption.WheelRage.ToString();
			
            // 디폴트 부분
            Check_AutoHidden.IsChecked = _DefaultOption.AutoHidden;
            Check_AutoStart.IsChecked = _DefaultOption.AutoStart;
            Check_SignalCheck.IsChecked = _DefaultOption.SignalCheck;

            Check_AutoComplete_Click(null, null);
            Check_Language_Click(null, null);
        }

        private void ShowCanvasKoreanDefault()
        {
            if ((bool)Check_Language.IsChecked)
            {
                Canvas_KoreanDefault.Visibility = Visibility.Visible;
                Canvas_KoreanShift.Visibility = Visibility.Hidden;
                Canvas_EnglishLower.Visibility = Visibility.Hidden;
                Canvas_EnglishUpper.Visibility = Visibility.Hidden;
                Canvas_Special1.Visibility = Visibility.Hidden;
                Canvas_Special2.Visibility = Visibility.Hidden;
                Canvas_Function.Visibility = Visibility.Hidden;
                Canvas_Browser.Visibility = Visibility.Hidden;
            }
            else
            {
                Canvas_KoreanDefault.Visibility = Visibility.Hidden;
                Canvas_KoreanShift.Visibility = Visibility.Hidden;
                Canvas_EnglishLower.Visibility = Visibility.Visible;
                Canvas_EnglishUpper.Visibility = Visibility.Hidden;
                Canvas_Special1.Visibility = Visibility.Hidden;
                Canvas_Special2.Visibility = Visibility.Hidden;
                Canvas_Function.Visibility = Visibility.Hidden;
                Canvas_Browser.Visibility = Visibility.Hidden;
            }
        }

        private delegate void SetBtnBackgroundOpacityDelegate(System.Windows.Controls.Button btn);

        private void ChangeKeyboardBackground(System.Windows.Controls.Button btn)
        {
            if ((bool)Check_AutoComplete.IsChecked)
            {
                //Btn_AutoCompleteOnOff.TabIndex = 50;
                btn.Dispatcher.BeginInvoke(new SetBtnBackgroundOpacityDelegate(SetBtnBackgroundOpacityMax), btn);
            }
            else
            {
                //Btn_AutoCompleteOnOff.TabIndex = 100;
                btn.Dispatcher.BeginInvoke(new SetBtnBackgroundOpacityDelegate(SetBtnBackgroundOpacityMin), btn);
            }
        }

        private void SetBtnBackgroundOpacityMax(System.Windows.Controls.Button btn)
        {
            (btn.Template.FindName("imgBtnBackground_Copy", btn) as Image).Opacity = 100;
        }
        private void SetBtnBackgroundOpacityMin(System.Windows.Controls.Button btn)
        {
            (btn.Template.FindName("imgBtnBackground_Copy", btn) as Image).Opacity = 0;
        }

        private void UserControl_Loaded ( object sender, RoutedEventArgs e )
        {

        }

        
	}
}